﻿using System;
using System.Web.Security;
using DataLib.Accounts.Bus;
using log4net;

namespace DataLib.Accounts
{
    public partial class Login : System.Web.UI.Page
    {
        ILog log = log4net.LogManager.GetLogger("Logger");
        protected void Page_Load(object sender, EventArgs e)
        {
            //专做测试用
            //string [] arr = "wowo".Split('!');
            //for(int i=0;i<arr.Length;i++)
            //{
            //    lblMsg.InnerText = arr[i] + "_" + i.ToString();
            //}
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
            {
                int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);

            }

            string userName = DataLib.Common.PageValidate.InputText(txtun.Value.Trim(), 30);
            string Password = DataLib.Common.PageValidate.InputText(txtpwd.Value.Trim(), 30);
            string userip = GetIp();
            AccountsPrincipal newUser = AccountsPrincipal.ValidateLogin(userName, Password);
            if (newUser == null)
            {
                log.Info("登陆失败，帐号：" + userName + "; IP地址：" + userip);
                if (string.IsNullOrWhiteSpace(userName))
                {
                   
                    dialog1.Visible = true;
                    dialog2.Style["left"] = "260px";
                }
                else
                {
                  
                    dialog1.Visible = false;
                    dialog2.Style["left"] = "360px";
                }
                if (string.IsNullOrWhiteSpace(Password))
                {
                    dialog2.Visible = true;
                }
                else
                {
                    dialog2.Visible = false;
                }
                //login_error.Visible = true;
                
                //lblMsg.InnerText = "登陆失败： " + userName;
                if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
                {
                    int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
                    Session["PassErrorCountAdmin"] = PassErroeCount + 1;
                }
                else
                {
                    Session["PassErrorCountAdmin"] = 1;
                }
            }
            else
            {
                User currentUser = new DataLib.Accounts.Bus.User(newUser);

                log.Info("登陆成功，帐号：" + userName + "; IP地址：" + userip);
                //if (currentUser.UserType != "AA")
                //{
                //    this.lblMsg.Text = "你非管理员用户，你没有权限登录后台系统！";
                //    return;
                //}
                Context.User = newUser;
                Common.ConfigHelper.username = userName;
                if (((SiteIdentity)User.Identity).TestPassword(Password) == 0)
                {
                    //lblMsg.InnerText = "你的密码无效！";
                    if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
                    {
                        int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
                        Session["PassErrorCountAdmin"] = PassErroeCount + 1;
                    }
                    else
                    {
                        Session["PassErrorCountAdmin"] = 1;
                    }
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(userName, false);
                    //日志
                    //UserLog.AddLog(currentUser.UserName, currentUser.UserType, Request.UserHostAddress, Request.Url.AbsoluteUri, "登录成功");

                    Session["UserInfo"] = currentUser;
                    //Session["Style"] = currentUser.Style;
                    if (Session["returnPage"] != null)
                    {
                        string returnpage = Session["returnPage"].ToString();
                        Session["returnPage"] = null;
                        Response.Redirect(returnpage);
                    }
                    else
                    {
                        Response.Redirect("/main.aspx");
                    }
                }
            }
        }


        string GetIp()
        {

            //可以透过代理服务器

            string userIP = Request.UserHostAddress;

            if (userIP == null || userIP == "")
            {

                //没有代理服务器,如果有代理服务器获取的是代理服务器的IP

                userIP = Request.ServerVariables["REMOTE_ADDR"];

            }

            return userIP;

        }



    }
}